<template>
    <div class="com-container">
        <div class="title" :style="titleStyle">
            <div class="title-icon" @click="showChoice = !showChoice">
                <span>{{ '|&nbsp;' + showTitle }}</span>
                <span> ^ </span>
            </div>
            <!-- v-show="showChoice" v-for="item in selectType" :key="item.key" {{item}} -->
            <div class="title_item" :style="marginStyle" v-show="showChoice" v-for="item in selectType" :key="item.key"
                @click="selectItem(item)">
                {{ item.text }}
            </div>
        </div>
        <div class="com-chart" ref="ref_trend"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
    data() {
        return {
            echartsInit: null,//初始化echaarts
            allData: null,//初始化数据
            showChoice: false, // 是否显示可选项
            choiceType: 'map', // 显示的数据类型
            titleFontSize: 0,// 指明标题的字体大小

        }
    },
    created() {
        this.$ws.registerCallBack('trendData', this.getData)
    },
    //钩子函数  加载完bom是的钩子函数
    mounted() {
        this.initEchartes();
        //this.getData();
        this.$ws.send({
            action: 'getData',
            socketType: 'trendData',
            chartName: 'trend'
        })
        window.addEventListener("resize", this.resizeEcharts);
        this.resizeEcharts();
    },
    destroyed() {
        //销毁实例  防止内存泄漏
        window.removeEventListener('resize', this.resizeEcharts)
        //销毁实例 trendData
        this.$ws.unRegisterCallBack('trendData')
    },
    //书写方法的地方
    methods: {
        //初始化echarts
        initEchartes() {

            this.echartsInit = this.$echarts.init(this.$refs.ref_trend, this.theme)
            const initOption = {
                xAxis: {
                    type: 'category',
                    boundaryGap: false//折线从坐标轴开始延伸
                },
                yAxis: {
                    type: 'value'
                },
                grid: {
                    //控制坐标轴的大小
                    left: '3%',
                    top: '35%',
                    right: '4%',
                    bottom: '1%',
                    containLabel: true//从文字开始进行边距的变化
                },
                legend: {
                    left: 20,
                    top: '15%',
                    icon: 'circle'
                },
                tooltip: {
                    trigger: 'axis'
                }
            }
            this.echartsInit.setOption(initOption);
        },
        //获取数据方法
        async getData() {
            const { data } = await this.$http.get('trend');
            //console.log(data)
            this.allData = data
            //更新数据
            this.updataEcharts();
            console.log(this.allData.type)
        },
        //更新echarts
        updataEcharts() {
            // 半透明的颜色值
            const colorArr1 = [
                'rgba(11, 168, 44, 0.5)',
                'rgba(44, 110, 255, 0.5)',
                'rgba(22, 242, 217, 0.5)',
                'rgba(254, 33, 30, 0.5)',
                'rgba(250, 105, 0, 0.5)'
            ]
            // 全透明的颜色值
            const colorArr2 = [
                'rgba(11, 168, 44, 0)',
                'rgba(44, 110, 255, 0)',
                'rgba(22, 242, 217, 0)',
                'rgba(254, 33, 30, 0)',
                'rgba(250, 105, 0, 0)'
            ]
            //处理数据
            //类目轴的数据
            const timeArr = this.allData.common.month;
            //y轴的数据
            const valueArr = this.allData[this.choiceType].data;
            //图例数据
            const legendArr = valueArr.map(item => {
                return item.name
            })
            const seriesArr = valueArr.map((item, index) => {
                return {
                    name: item.name,
                    type: 'line',
                    data: item.data,
                    stack: this.choiceType,//将五条数据实现堆叠图
                    areaStyle: {//折线与坐标轴围成的面积
                        color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            {
                                //%0的颜色值
                                offset: 0,
                                color: colorArr1[index]
                            },
                            {
                                //100%的颜色值
                                offset: 1,
                                color: colorArr2[index]
                            }
                        ])

                    }
                }
            })
            // console.log(valueArr)
            const updataOption = {
                xAxis: {
                    data: timeArr
                },
                series: seriesArr,
                legend: {
                    data: legendArr
                },

            }
            this.echartsInit.setOption(updataOption);
        },
        //自适应echarts
        resizeEcharts() {
            //console.log(this.$refs.ref_trend.offsetWidth)
            this.titleFontSize = this.$refs.ref_trend.offsetWidth / 100 * 3.6;
            console.log(this.titleFontSize)
            const resizeOptino = {
                legend: {
                    itemWidth: this.titleFontSize,
                    itemHeight: this.titleFontSize,
                    itemGap: this.titleFontSize,
                    textStyle: {
                        fontSize: this.titleFontSize / 2
                    }
                }
            }
            this.echartsInit.setOption(resizeOptino)
            this.echartsInit.resize();

        },
        //选着元素的方法
        selectItem(item) {
            this.choiceType = item.key;
            this.updataEcharts()
            this.showChoice = !this.showChoice
        },

    },
    computed: {
        selectType() {
            if (!this.allData) {
                return []
            } else {
                return this.allData.type.filter(item => {
                    return item.key !== this.choiceType
                })
            }
        },
        showTitle() {
            if (!this.allData) {
                return ''
            } else {
                return this.allData[this.choiceType].title
            }
        },
        //标题样式
        titleStyle() {
            return {
                fontSize: this.titleFontSize + 'px',
                color: getThemeValue(this.theme).titleColor
            }
        },
        //控制margin
        marginStyle() {
            return {
                marginLeft: this.titleFontSize + 'px',
                backgroundColor: getThemeValue(this.theme).backgroundColor,
                color: getThemeValue(this.theme).titleColor

            }
        },
        ...mapState(['theme'])
    },

    watch: {
        theme() {
            this.echartsInit.dispose()
            this.initEchartes()
            this.resizeEcharts()
            this.updataEcharts()
        }
    },
}
</script>

<style lang="less" >
.title {
    position: absolute;
    color: #fff;
    z-index: 2;
    left: 20px;
    top: 20px;

    .title-icon {
        cursor: pointer;
    }

    .title_item {
        background-color: #222733;
    }
}
</style>